// 登录、注册
const express = require("express"); //引入express
const router = express.Router();

const Profile = require("../../models/profile");
const passport =require('passport');
// 测试接口
router.get("/test", (req, res) => {
  res.json({ msg: "Profile works" });
});
const auth=require('../../config.js/auth');
// 添加
router.post("/add",auth ,(req, res) => {
   console.log('req.body',req.body);
    let profileFields={
        type:req.body.type,
        describe:req.body.describe,
        income:req.body.income,
        expend:req.body.expend,
        cash:req.body.cash,
        remark:req.body.remark
    }
    new Profile(profileFields).save().then(profile=>{
        res.json({
            data:profile,
            msg:'添加成功'
        })
    })
})


// 查询所有信息
router.get("/find",auth,(req, res) => {
    Profile.find().then(profile=>{
        console.log(profile);
        if (profile.length==0){
               return res.status(404) .json("没有内容")
        }
        res.json(profile)
    }).catch(err=>res.status(404).json(err))
})


// 查询单个信息
router.get("/findOne/:id",auth,(req, res) => {
    console.log(req.params);
    Profile.findOne({_id:req.params.id}).then(profile=>{
        if (profile.length==0){
               return res.status(404) .json("没有内容")
        }
        res.json(profile)
    }).catch(err=>res.status(404).json(err))
})

// 修改
router.post("/edit/:id",auth,(req, res) => {
    let profileFields={
        type:req.body.type,
        describe:req.body.describe,
        income:req.body.income,
        expend:req.body.expend,
        cash:req.body.cash,
        remark:req.body.remark
    }
    Profile.findOneAndUpdate(
        {_id:req.params.id},
        {$set:profileFields},
        {new:true}
    ).then(profile=>res.json(profile)).catch(err=>res.json(err))
})

// 删除
router.delete("/delete/:id",auth,(req, res) => {
    Profile.findByIdAndDelete({_id:req.params.id}).then(profile=>{
       console.log(profile);
        res.json(profile)
    }).catch(err=>res.status(404).json(err))
})

module.exports = router;
